<?php
    include_once "../lib/BLL/DmNganhBLL.php";
    include_once "../lib/BLL/ChuongTrinhDaotaoBLL.php";
    include_once "../lib/BLL/DmHeDaoTaoBLL.php";
    include_once "../lib/BLL/DmTrinhDoBLL.php";
    
    $ctdt_dal=new ChuongTrinhDaoTao();
    $ctdt_bll=new ChuongTrinhDaoTaoBLL();
    $hdt_bll=new DmHeDaoTaoBLL();
    $trinhdo_bll=new DmTrinhDoBLL();
    $nganh_bll=new DmNganhBLL();
    $tt="";
    $ma_ctdt="";
    $madm_hdt="";
    $madm_td="";
    $madm_nganh="";
    $btnName="Thêm";
    if (isset($_POST['timkiem'])) {
        $keysearch="";
		$where="";
		if(isset($_POST['txtkey']))
		{
			$keysearch=$_POST['txtkey'];
			$where="WHERE".$ctdt_bll->buildSearchQueryLike($keysearch);
        }
    }
    if(!isset($_POST['timkiem'])||($_POST['txtkey']=="")){
        $where="WHERE 1=1";
    }
    if(isset($_GET['ma'])){
        $btnName="Sửa";
        $mactdt=$_GET['ma'];
        $rs=$ctdt_bll->dulieutheoma($mactdt);
        $ma_ctdt=$rs['ma_ctdt'];
        $madm_td=$rs['madm_td'];
        $madm_hdt=$rs['madm_hdt'];
        $madm_nganh=$rs['madm_nganh'];
        $tt="readonly";
    }
    
    if(isset($_POST['btnThem'])){
        $ma_ctdt=$_POST['ma_ctdt'];
        $madm_td=$_POST['madm_td'];
        $madm_hdt=$_POST['madm_hdt'];
        $madm_nganh=$_POST['madm_nganh'];
        if($madm_td!="-1"&&$madm_hdt!="-1"&&$madm_nganh!="-1"&&$ma_ctdt!=""){
            if($btnName=="Thêm"){
                $ctdt_dal->set_tbl_ctdt($ma_ctdt,$madm_td,$madm_hdt,$madm_nganh);
                $kq=$ctdt_bll->themCTDT($ctdt_dal);
                if($kq){
                    header("location: chuongtrinhdaotao-ctdt");
                }
            }
            else{
                $ctdt_dal->set_tbl_ctdt($mactdt,$madm_td,$madm_hdt,$madm_nganh);
                $kq=$ctdt_bll->suaCTDT($ctdt_dal);
                if($kq){
                    header("location: chuongtrinhdaotao-ctdt");
                }
            }
        }
    }
    if(isset($_GET['xoama'])){
        $xoama_ctdt=$_GET['xoama'];
        $kq=$ctdt_bll->xoaCTDT($xoama_ctdt);
        header("location: chuongtrinhdaotao-ctdt");
    }
    if(isset($_POST['xoachon'])){
        if(isset($_POST['checkall'])){
            $kq=$ctdt_bll->xoaChonCTDTAll();
            if ($kq == true){
                header("location: chuongtrinhdaotao-ctdt");
            }
        }
        if(isset($_POST['checkbox'])){
            $delete=$_POST['checkbox'];
            foreach($delete as $ma_ctdt){
                $kq=$ctdt_bll->xoaCTDT($ma_ctdt);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-ctdt");
                }
            }
        }
    }
    
    //$ds_hdt->getListHeDaoTao();
    $ds_ctdt=$ctdt_bll->timkiem($where);
    $totalRecords=count($ds_ctdt);        
    $p=(isset($_GET['p']))?$_GET['p']:1;
    $limit=35;                       
    $offset=($p-1)*$limit;
    $ds_ctdt=$ctdt_bll->getPages($where,$offset,$limit);
    $url="chuongtrinhdaotao-ctdt";
    $pages=$ctdt_bll->paging($totalRecords,$limit,$url,'pagers');  

    $ds_hdt=$hdt_bll->getListHeDaoTao();
    $ds_td=$trinhdo_bll->GetlistTrinhDo();
    $ds_nganh=$nganh_bll->getListNganh();
    
    
    $smarty->assign('p',$p);
    $smarty->assign('pages',$pages);
    $smarty->assign('tt',$tt);
    $smarty->assign('btnName',$btnName);
    $smarty->assign('ma_ctdt',$ma_ctdt);
    $smarty->assign('madm_td',$madm_td);
    $smarty->assign('madm_hdt',$madm_hdt);
    $smarty->assign('madm_nganh',$madm_nganh);
    $smarty->assign('ds_ctdt',$ds_ctdt);
    $smarty->assign('ds_hdt',$ds_hdt);
    $smarty->assign('ds_nganh',$ds_nganh);
    $smarty->assign('ds_td',$ds_td);
    $smarty->display("chuongtrinhdaotao/ctdt.tpl");
?>